/*
 * This class uses
 */

package gui;

import database.Database;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JPasswordField;

/**
 *
 * @author Scott
 */
public class AddUserListener extends JDialog implements ActionListener{

    Database db;
    JFrame mainFrame;
    RosterListPanes rlp;
    JPasswordField passField;
    JTextField idField;
    JPanel inputPanel;


    public AddUserListener(Database db, JFrame main, RosterListPanes rlp){
        this.db = db;
        this.mainFrame = main;
        this.rlp = rlp;
    }

    private void createForm(){

        inputPanel = new JPanel();
        idField = new JTextField();
        JLabel idLabel = new JLabel("ID: ");
        JButton submit = new JButton("Submit");
        JLabel passLabel = new JLabel("Password: ");
        passField = new JPasswordField();

        submit.addActionListener(new SubmitUserListener());
        idField.setPreferredSize(new Dimension(150,25));
        passField.setPreferredSize(new Dimension(150, 25));

        inputPanel.setLayout(new FlowLayout());
        inputPanel.add(idLabel);
        inputPanel.add(idField);
        inputPanel.add(passLabel);
        inputPanel.add(passField);
        inputPanel.add(submit);

        inputPanel.setVisible(true);


        mainFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);
        mainFrame.pack();
        mainFrame.setSize(400, 600);
        mainFrame.setLocationRelativeTo(null);

    }

    private void clearForm(){
        inputPanel.setVisible(false);
        mainFrame.pack();
        mainFrame.setSize(400, 600);
        mainFrame.setLocationRelativeTo(null);
    }


    public void actionPerformed(ActionEvent e) {

            createForm();

    }
    
    class SubmitUserListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            db.addAdministrator(idField.getText(), passField.getSelectedText());
            clearForm();
            rlp.updateAdminPane();

        }
        
    }

}
